10 class ApiBlockTest
extends ApiTestCase
{
11 protected $mUser = null;
13 protected function setUp() {
15 $this->tablesUsed
= array_merge(
17 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
20 $this->mUser
= $this->getMutableTestUser()->getUser();
23 protected function getTokens() {
24 return $this->getTokenList( self
::$users['sysop'] );
28 * @param array $extraParams Extra API parameters to pass to doApiRequest
29 * @param User $blocker User to do the blocking, null to pick
32 private function doBlock( array $extraParams = [], User
$blocker = null ) {
33 if ( $blocker === null ) {
34 $blocker = self
::$users['sysop']->getUser();
37 $tokens = $this->getTokens();
39 $this->assertNotNull( $this->mUser
, 'Sanity check' );
40 $this->assertNotSame( 0, $this->mUser
->getId(), 'Sanity check' );
42 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
46 'user' => $this->mUser
->getName(),
47 'reason' => 'Some reason',
48 'token' => $tokens['blocktoken'],
50 if ( array_key_exists( 'userid', $extraParams ) ) {
51 // Make sure we don't have both user and userid
52 unset( $params['user'] );
54 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
57 $block = Block
::newFromTarget( $this->mUser
->getName() );
59 $this->assertTrue( !is_null( $block ), 'Block is valid' );
61 $this->assertSame( $this->mUser
->getName(), (string)$block->getTarget() );
62 $this->assertSame( 'Some reason', $block->mReason
);
70 public function testNormalBlock() {
77 public function testBlockById() {
78 $this->doBlock( [ 'userid' => $this->mUser
->getId() ] );
82 * A blocked user can't block
84 public function testBlockByBlockedUser() {
85 $this->setExpectedException( ApiUsageException
::class,
86 'You cannot block or unblock other users because you are yourself blocked.' );
88 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
90 'address' => $blocked->getName(),
91 'by' => self
::$users['sysop']->getUser()->getId(),
92 'reason' => 'Capriciousness',
93 'timestamp' => '19370101000000',
94 'expiry' => 'infinity',
98 $this->doBlock( [], $blocked );
101 public function testBlockOfNonexistentUser() {
102 $this->setExpectedException( ApiUsageException
::class,
103 'There is no user by the name "Nonexistent". Check your spelling.' );
105 $this->doBlock( [ 'user' => 'Nonexistent' ] );
108 public function testBlockOfNonexistentUserId() {
110 $this->setExpectedException( ApiUsageException
::class,
111 "There is no user with ID $id." );
113 $this->assertFalse( User
::whoIs( $id ), 'Sanity check' );
115 $this->doBlock( [ 'userid' => $id ] );
118 public function testBlockWithTag() {
119 ChangeTags
::defineTag( 'custom tag' );
121 $this->doBlock( [ 'tags' => 'custom tag' ] );
123 $dbw = wfGetDB( DB_MASTER
);
124 $this->assertSame( 1, (int)$dbw->selectField(
125 [ 'change_tag', 'logging', 'change_tag_def' ],
127 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
131 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
132 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ],
137 public function testBlockWithProhibitedTag() {
138 $this->setExpectedException( ApiUsageException
::class,
139 'You do not have permission to apply change tags along with your changes.' );
141 ChangeTags
::defineTag( 'custom tag' );
143 $this->setMwGlobals( 'wgRevokePermissions',
144 [ 'user' => [ 'applychangetags' => true ] ] );
146 $this->doBlock( [ 'tags' => 'custom tag' ] );
149 public function testBlockWithHide() {
150 global $wgGroupPermissions;
151 $newPermissions = $wgGroupPermissions['sysop'];
152 $newPermissions['hideuser'] = true;
153 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
154 [ 'sysop' => $newPermissions ] );
156 $res = $this->doBlock( [ 'hidename' => '' ] );
158 $dbw = wfGetDB( DB_MASTER
);
159 $this->assertSame( '1', $dbw->selectField(
162 [ 'ipb_id' => $res[0]['block']['id'] ],
167 public function testBlockWithProhibitedHide() {
168 $this->setExpectedException( ApiUsageException
::class,
169 "You don't have permission to hide user names from the block log." );
171 $this->doBlock( [ 'hidename' => '' ] );
174 public function testBlockWithEmailBlock() {
175 $res = $this->doBlock( [ 'noemail' => '' ] );
177 $dbw = wfGetDB( DB_MASTER
);
178 $this->assertSame( '1', $dbw->selectField(
181 [ 'ipb_id' => $res[0]['block']['id'] ],
186 public function testBlockWithProhibitedEmailBlock() {
187 $this->setExpectedException( ApiUsageException
::class,
188 "You don't have permission to block users from sending email through the wiki." );
190 $this->setMwGlobals( 'wgRevokePermissions',
191 [ 'sysop' => [ 'blockemail' => true ] ] );
193 $this->doBlock( [ 'noemail' => '' ] );
196 public function testBlockWithExpiry() {
197 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
199 $dbw = wfGetDB( DB_MASTER
);
200 $expiry = $dbw->selectField(
203 [ 'ipb_id' => $res[0]['block']['id'] ],
207 // Allow flakiness up to one second
208 $this->assertLessThanOrEqual( 1,
209 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
212 public function testBlockWithInvalidExpiry() {
213 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
215 $this->doBlock( [ 'expiry' => '' ] );
218 public function testBlockWithRestrictions() {
219 $this->setMwGlobals( [
220 'wgEnablePartialBlocks' => true,
224 $page = $this->getExistingTestPage( $title );
228 'pagerestrictions' => $title,
231 $block = Block
::newFromTarget( $this->mUser
->getName() );
233 $this->assertFalse( $block->isSitewide() );
234 $this->assertCount( 1, $block->getRestrictions() );
235 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
239 * @expectedException ApiUsageException
240 * @expectedExceptionMessage The "token" parameter must be set
242 public function testBlockingActionWithNoToken() {
246 'user' => $this->mUser
->getName(),
247 'reason' => 'Some reason',
251 self
::$users['sysop']->getUser()
256 * @expectedException ApiUsageException
257 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
259 public function testBlockWithLargeRange() {
260 $tokens = $this->getTokens();
265 'user' => '127.0.0.1/64',
266 'reason' => 'Some reason',
267 'token' => $tokens['blocktoken'],
271 self
::$users['sysop']->getUser()
276 * @expectedException ApiUsageException
277 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
280 public function testBlockingToManyRestrictions() {
281 $this->setMwGlobals( [
282 'wgEnablePartialBlocks' => true,
285 $tokens = $this->getTokens();
290 'user' => $this->mUser
->getName(),
291 'reason' => 'Some reason',
293 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
294 'token' => $tokens['blocktoken'],
298 self
::$users['sysop']->getUser()